package com.vic.test.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
 * 用JDK提供的动态代理方案写一个动态代理
 * @author VIC
 *
 */
public class DynamicProxy implements InvocationHandler{

	private Object target;
	
	public <T> T getProxy(){
		return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
	}
	
	
	public DynamicProxy(Object target) {
		super();
		this.target = target;
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		before();
		Object result = method.invoke(target, args);
		after();
		return result;
	}
	
	
	public void before(){
		System.out.println("before");
	}
	
	public void after(){
		System.out.println("after");
	}


}
